﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
using DTO.InnerException;
using BLL.Util;

namespace MZ
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowUserInfo();
            }
        }

        protected void BtnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                AccountBLL.Login(TxtAccount.Text, TxtPassword.Text);                
            }
            catch (NoAccountException naex)
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "error", "alert('" + naex.Message + "')", true);
            }
            catch (AccountErrorException aeex)
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "error", "alert('" + aeex.Message + "')", true);
            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "error", "alert('" + ex.Message + "')", true);
            }
        }

        protected void BtnLogout_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            FormsAuthentication.RedirectToLoginPage();
        }

        protected void BtnReg_Click(object sender, EventArgs e)
        {
           var account = AccountBLL.Register(TxtRegEmail.Text, TxtRegPassword.Text, TxtRegAccount.Text);
           FormsAuthentication.SetAuthCookie(account.AccountName, false);
           Session.Add("account", account);
           Response.Redirect("~/default.aspx");
        }

        private void ShowUserInfo()
        {
            UserInfo.Visible = User.Identity.IsAuthenticated;
            if (User.Identity.IsAuthenticated)
            {
                var account= CacheHelper.GetAccount(User.Identity.Name);
                UserInfo.InnerText = account.AccountName + "欢迎您再次到来 ^_^";
            }
        }
    }
}
